import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;


public class d2b {


    public static String StatList(String str) {
        StringBuffer sb = new StringBuffer();
        HashMap<String ,Integer> has = new HashMap<String ,Integer> ();
        String[] slist = str.split("[^a-zA-Z\']+");
        for (int i = 0; i < slist.length; i++)
        {
            if (!has.containsKey(slist[i]))
            {
                has.put(slist[i], 1);
            }
            else
            {
                has.put(slist[i],has.get(slist[i])+1 );
            }
        }
        Iterator<String> iterator = has.keySet().iterator();
        String a[]=new String[100];
        int s[]=new int[100];
        int n=10;
        int judge;
        System.out.println("输入前n个最常出现的单词:");
        for(int i=0;i<n;i++)
        {
            iterator = has.keySet().iterator();
            while(iterator.hasNext())
            {
                String word = (String) iterator.next();
                if(s[i]<has.get(word))
                {
                    s[i]=has.get(word);
                    a[i]=word;
                }
            }
            judge=woor(a[i]);
            if(judge==1)
            {
                n++;
                has.remove(a[i]);
            }
            else
            {
                sb.append("单词：").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n");
                has.remove(a[i]);
            }
        }
        return sb.toString();
    }


    public static void main(String[] args)
    {
        File file = new File(args[1]);

        ArrayList<String> fil=getDirectory(file);
        for(int i=0;i<fil.size();i++)
        {
            String filePath = fil.get(i);
            String sz=readTxtFile(filePath);
            String ltxt=null;
            System.out.println(ltxt=StatList(sz));

        }

    }
    public static void daochu(String a) throws IOException
    {
        File file=new File(a);
        FileOutputStream fos=new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
        osw.append(a);
        osw.close();
        fos.close();
    }

    public static int woor(String a)
    {
        int n=0;
        String[] biao= {"a","the","an","and"};
        for(int i=0;i<biao.length;i++)
        {
            if(biao[i].equals(a))
            {
                n=1;
            }
        }
        return n;
    }

    public static ArrayList<String> getFiles(String path) {
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
            }
        }
        return files;
    }

    private static ArrayList<String> getDirectory(File file) {
        ArrayList<String> files = new ArrayList<String>();
        ArrayList<String> files1 = new ArrayList<String>();

        File flist[] = file.listFiles();
        for (File f : flist) {
            if (f.isDirectory()) {

                files1=getFiles(f.getAbsolutePath());
                files.addAll(files1);

                getDirectory(f);
            } else {
                files.add(f.getAbsolutePath());
            }
        }
        return files;
    }

    public static String readTxtFile(String filePath){
        try {
            String encoding="GBK";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                String lineText="";
                while((lineTxt = bufferedReader.readLine()) != null)
                {
                    lineText+=(lineTxt);
                }
                read.close();
                return lineText;
            }
            else
            {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return null;
    }

}